package com.bookmngsys.controller;

import com.alipay.api.AlipayApiException;
import com.bookmngsys.dao.FineHistoryDao;
import com.bookmngsys.entity.AliPayBean;
import com.bookmngsys.entity.BookInfo;
import com.bookmngsys.entity.FineHistory;
import com.bookmngsys.service.BookInfoService;
import com.bookmngsys.service.BookService;
import com.bookmngsys.service.PayService;
import com.bookmngsys.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Random;

@Controller
@RequestMapping("/ali")
public class OrderController {

    /**日志对象*/
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private PayService payService;

    @Autowired
    BookInfoService bookInfoService;

    @Autowired
    BookService bookService;

    @Autowired
    FineHistoryDao fineHistoryDao;

    @RequestMapping("/pay")
    @ResponseBody
    public String alipay(@RequestParam String bookId) throws Exception {
        String subject = "PAYFORFINE";
        BookInfo bookInfo = null;
        try{
            bookInfo = bookInfoService.getBookById(Integer.valueOf(bookId));
        }catch (Exception e){
            return "some error";
        }
        if(bookInfo == null || bookInfo.getBorrowReaderId() == null){
            return "can not find borrow copy";
        }
        long money = (long)CommonUtils.getFineAmount(bookInfo.getReturnDate());
        if(money == 0){
            throw new Exception("you not overdue");
        }
        String totalAmount = String.valueOf(money);
        Long t = System.currentTimeMillis();
        String outTradeNo = t.toString().substring(0,5) +  t.toString().substring(8,t.toString().length()) + bookId;
        logger.info("商户订单号为{},订单名称为{},付款金额为{}", outTradeNo, subject, totalAmount);
        AliPayBean alipayBean = new AliPayBean();
        alipayBean.setOut_trade_no(outTradeNo);
        alipayBean.setSubject(subject);
        alipayBean.setTotal_amount(totalAmount);
        String res = payService.aliPay(alipayBean);
        //bookService.payMoney(Integer.valueOf(bookId));
        long endDate = bookInfo.getReturnDate();
        {
            //付款账单同步
            FineHistory a = new FineHistory();
            a.setIsbn(bookInfo.getIsbn());
            a.setReaderId(bookInfo.getBorrowReaderId());
            a.setBookId(bookInfo.getBookId());
            a.setId((new Random(System.currentTimeMillis())).nextInt(Integer.MAX_VALUE));
            a.setDiscription("overdue payment");
            a.setFineAmount((float)money);
            a.setPayFineDate(System.currentTimeMillis());
            a.setOutTradeNo(outTradeNo);
            fineHistoryDao.save(a);
        }
        return res;
    }

    @RequestMapping("/success")
    @ResponseBody
    public String success(){
        return "交易成功！";
    }

    @RequestMapping(value = "/index")
    public String payCoin(){
        return "zfindex.html";
    }
}
